﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo;
using CanDoo.Core;
using CanDoo.Core.Data;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.DTOs;

namespace CanDoo.Oil.Core.Services
{
    /// <summary>
    /// 业务实现——应付账单
    /// </summary>
    public class BuyBillService : CanDoo.Data.Entity.Services.ServiceBase<BuyBill>, IBuyBillContract
    {
        public BuyBillService()
        {
        
        }

        public string GetNewCode()
        {
            var startStr = "YFZD";
            var dateStr = DateTime.Now.ToString("yyMMdd");
            var codePrefix = startStr + dateStr;

            //生成新单据号
            var newCode = "";
            var entity = Entities.Where(p => p.Code.StartsWith(codePrefix)).OrderByDescending(p => p.Code).FirstOrDefault();
            if (entity == null)
                newCode = codePrefix + "0001";
            else
                newCode = startStr + (int.Parse(entity.Code.Substring(startStr.Length)) + 1);
            return newCode;
        }

        public OperationResult CreateBill(InBuyBack m)
        {
            var result = OperationResult.Success;

            var entity = new BuyBill();
            entity.Id = IdMaker.NewId();
            entity.Code = GetNewCode();
            entity.Name = "采购退货单[" + m.Code + "]账单";
            entity.SupplierId = m.SupplierId;
            entity.Money = (m.Money - 0)*-1;
            entity.CreateDate = System.DateTime.Now;
            entity.BuyBillType = BuyBillTypeEnum.InStore;
            entity.WaitMoney = (m.Money - 0) * -1;
            entity.PayedMoney = 0;
            entity.NeedPayDate = System.DateTime.Now;
            entity.PayState = 0;
            entity.LinkEntityId = m.Id;
            entity.LinkMoney = m.Money;
            entity.LinkPayedMoney = 0;
            entity.Remark = "";
            AddEntity(entity);

            return result;
        }

        public OperationResult CreateBill(InStore m)
        {
            var result = OperationResult.Success;

            var entity = new BuyBill();
            entity.Id = IdMaker.NewId();
            entity.Code = GetNewCode();
            entity.Name = "采购单[" + m.Code + "]账单";
            entity.SupplierId = m.SupplierId;
            entity.Money = m.Money-m.CashPayed;
            entity.CreateDate = System.DateTime.Now;
            entity.BuyBillType = BuyBillTypeEnum.InBuyBack;
            entity.WaitMoney = m.Money - m.CashPayed; ;
            entity.PayedMoney = 0;
            entity.NeedPayDate = m.AppointPayDate;
            entity.PayState = 0;
            entity.LinkEntityId = m.Id;
            entity.LinkMoney = m.Money;
            entity.LinkPayedMoney = m.CashPayed;
            entity.Remark = "";
            AddEntity(entity);

            return result;
        }

        public OperationResult DeleteBill(long linkEntityId)
        {
            using (var scope = DbScopeFactory.Create())
            {
                var entity = Entities.FirstOrDefault(p => p.LinkEntityId == linkEntityId);
                if (entity != null)
                {
                    if (entity.PayState > 0)
                        throw new Exception("结算单[" + entity.Code + "]已结算");
                    base.Recycle(entity);
                    DeleteEntity(entity.Id);
                }
            }
            return OperationResult.Success; ;
        }

        public StatementDetailItem EntityChange(BuyBill m)
        {
            var ItemEntity = new StatementDetailItem();
            ItemEntity.Id = "0";
            ItemEntity.Code = m.Code;
            ItemEntity.Name = m.Name;
            ItemEntity.BillId = m.Id.ToString();
            ItemEntity.NeedDate = m.NeedPayDate;
            ItemEntity.CreatedDate = m.CreateDate;
            ItemEntity.Money = m.Money;
            ItemEntity.PayedMoney = m.PayedMoney;
            ItemEntity.WaitMoney = m.WaitMoney;
            ItemEntity.thisPayMoney = m.WaitMoney;
            ItemEntity.thisPayedLastMonoey = 0;
            return ItemEntity;
        }
    }
}
